<?php 
    class Musers extends CI_Model{
        function __construct(){
            parent::__construct();
            $this->load->database();
        }
    
        public function validate(){
            // grab user input
            $username = $this->input->post('username');
            $password = $this->input->post('password');
            
            // Prep the query
            $this->db->where('users_Name', $username);
            $this->db->where('users_Pass', md5($password));
            
            $query = $this->db->get('users');
            if($query->num_rows == 1)
            {
                $row = $query->row();
                $data = array(
                        'users_Id' => $row->users_Id,
                        'users_Name' => $row->users_Name,
                        'users_FullName' => $row->users_FullName,
                        'users_Email' =>$row->users_Email
                        );
                $this->session->set_userdata($data);
                var_dump($data);
                return true;
            }
            return false;
        }
        public function insertUser($data) {
            $this->db->insert('users', $data); 
        }

        public function listAllUserNomal() {
            $this->db->select('*');
            $this->db->from('users');
            $this->db->join('userstype', 'users.users_Type = userstype.ut_Id', 'left');
            $this->db->where('users_Delete', 0);
            $this->db->where('users_Type', 1);
            $query = $this->db->get();
            return $query->result_array();
        
        }

        public function listAllUser() {
            $this->db->select('*');
            $this->db->from('users');
            $this->db->join('userstype', 'users.users_Type = userstype.ut_Id','left');
            $query = $this->db->get();
            return $query->result_array();
        
        }

        public function listAllUserBanNick(){
            $this->db->select('*');
            $this->db->from('users');
            $this->db->join('userstype', 'users.users_Type = userstype.ut_Id','left');
            $this->db->where('users_Delete', 1);
            $query = $this->db->get();
            return $query->result_array();
        
        }

        public function listAllUserAdmin(){
            $this->db->select('*');
            $this->db->from('users');
            $this->db->join('userstype', 'users.users_Type = userstype.ut_Id','left');
            $this->db->where('users_Type', 2);
            $query = $this->db->get();
            return $query->result_array();
        
        }

        public function validateAdmin($username, $password){
            // Prep the query
            $this->db->where('users_Name', $username);
            $this->db->where('users_Pass', md5($password));
            $this->db->where('users_Type', 2);

            $query = $this->db->get('users');
            if($query->num_rows == 1)
            {
                $row = $query->row();
                $data = array(
                        'users_Id_Admin' => $row->users_Id,
                        'users_Name_Admin' => $row->users_Name,
                        'users_FullName' => $row->users_FullName
                );

                $this->session->set_userdata($data);
                return true;
            }
            return false;
        }

        public function deleteUser($users_Id) {

            $data = array(
               'users_Delete' => 1
            );
            $this->db->where('users_Id', $users_Id);
            $this->db->update('users', $data); 
    
        }

    }
?>